from SeerGlobal import *
#from SeerFight import *
#task = get_value('Task')
#zhan = get_value('Zhan')
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *      #无边框,选框,动画
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

_path = ''
_path = sys.argv[0]
_path = os.path.dirname(_path)
if _path[-8:] == 'SeerMain':
    _path = str(_path[0:-8])
sys.path.append(_path + '\\List\\JieTu')#Yoso路径
sys.path.append('./')#当前相对路径

from pyJieTuWindow import Ui_MainWindow
from pyBianJiQiWindow import Ui_MainWindow as Ui_MainWindow2
import tuoZhan

class myTextEdit(QtWidgets.QTextEdit):
    def __init__(self,parent):
        QtWidgets.QTextEdit.__init__(self)
        self.parent=parent
        self.setCursorWidth(4)
        self._path = ''
    def keyPressEvent(self, event):
        if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_S:
            self.textBaoCun()
        if event.key() == Qt.Key_Space:
            QtWidgets.QTextEdit.keyPressEvent(self,event)
            QtWidgets.QTextEdit.keyPressEvent(self,event)
            QtWidgets.QTextEdit.keyPressEvent(self,event)
            QtWidgets.QTextEdit.keyPressEvent(self,event)
        else:
            QtWidgets.QTextEdit.keyPressEvent(self,event)
    
    def textBaoCun(self):
        """快捷键保存"""
        _str = self.toPlainText()
        f=open(self._path, 'w',encoding='utf-8')
        f.write(_str)
        QMessageBox.information(self,"提示","快捷键保存成功")
        pass

class BianJiQiWindows(Ui_MainWindow2,QMainWindow):
    def __init__(self,_path=''):
        super().__init__()
        self.setupUi(self)
        self._path = _path
        
        self.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowStaysOnTopHint)
        self.myTextEdit = myTextEdit(self.centralwidget)
        self.myTextEdit.setObjectName("myTextEdit")
        self.horizontalLayout.addWidget(self.myTextEdit)
        self.action.triggered.connect(self.baoCun)
    
    def baoCun(self):
        """保存代码"""
        _str = self.myTextEdit.toPlainText()
        f=open(self._path, 'w',encoding='utf-8')
        f.write(_str)
        pass
    
        
        
class JieTuWindows(Ui_MainWindow,QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowStaysOnTopHint)
        self.setFixedSize(135,150)
        self.pushButton.clicked.connect(self.createTask)
        self.pushButton_2.clicked.connect(self.openTask)
        self.pushButton_3.clicked.connect(self.startJieTu)
        self.pushButton_4.clicked.connect(self.createJoyPy)
        self.pushButton_5.clicked.connect(self.openJoyPy)
        
        self._path = ''
    
    def createTask(self):
        """创建任务文件夹"""
        _task = QFileDialog.getExistingDirectory(self,'任务将会创建在你现在选的文件夹之中  推荐选择List文件夹')#选择文件夹
        _str = tuoZhan.generate_random_str01(5)#获取随机的任务名
        isExists = os.path.exists(_task + '\\' + _str)#判断是否存在 任务目录，如果不存在就创建
        if(isExists == False):
            os.mkdir(_task + '\\' + _str)#创建空任务文件夹
        self._path = _task + '/' + _str
    
    def openTask(self):
        """打开任务文件夹"""
        _task = QFileDialog.getExistingDirectory(self,'选择你的任务文件夹 图片将会直接存在这个目录内  如fxv5q')#选择文件夹
        self._path = _task
    
    def startJieTu(self):
        """开始截图"""
        if self._path == '':
            QMessageBox.information(self,"提示","你还没选择任务目录")
            return
        isExists = os.path.exists(self._path)#判断是否存在 任务目录，如果不存在就创建(防止误删)
        if(isExists == False):
            os.mkdir(self._path)#创建空任务文件夹    
    
        _path = self._path
        _list = _path.split('/')
        _str = _list[len(_list)-1]
        tuoZhan.open_ScreenSpy()
        print(_list)
        print(_str)
        print(_path)
        tuoZhan.ScreenSpy(_str,_path)
        os.startfile(self._path)

    def createJoyPy(self):
        """创建脚本文件和通用的配置文件"""
        if self._path == '':
            QMessageBox.information(self,"提示","你还没选择任务目录")
            return
        _path1 = _path + '\\List\\JieTu\\JoyPy\\cfg.ini'
        _path2 = _path + '\\List\\JieTu\\JoyPy\\job.py'
        
        _path3 = self._path + '\\cfg.ini'#获取当前任务文件夹的路径
        _path4 = self._path + '\\job.py'#获取当前任务文件夹的路径
        tuoZhan.dm.CopyFile(_path1,_path3,1)
        tuoZhan.dm.CopyFile(_path2,_path4,1)
        _str = self._path[-5:]
        tuoZhan.dm.WriteIni("Plugin","task_id",_str,_path3)
    
    def openJoyPy(self):
        """打开脚本文件job.py"""
        if self._path == '':
            QMessageBox.information(self,"提示","你还没选择任务目录")
            return
        _path4 = self._path + '\\job.py'#获取当前任务文件夹的路径
        isExists = os.path.exists(_path4)#判断是否存在 joy.py，如果不存在就创建(防止误删)
        if(isExists == False):
            print(_path4 + "   job.py不存在")
            QMessageBox.information(self,"提示","job.py不存在")
            return  
        f=open(_path4, 'r',encoding='utf-8')
        _str = f.read()
        self.bianw=BianJiQiWindows(_path4)
        self.bianw.show()
        self.bianw.myTextEdit.setText(_str)
        self.bianw.myTextEdit._path = _path4
        

jobw=JieTuWindows()
jobw.show()

def job():
    """ 截图工具
        该文件是为了YoSo来启动程序
        所以job函数内不应该有代码
    """
    pass